<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
        <!--部门数据-->
        <pane size="16">
          <el-col class="pane-left">
            <div style="background-color: #6c8be9;padding: 16px;color: white;border-radius: 10px 10px 0 0;"
            >{{ classOptions[0].label }}
            </div>
            <!--            <div class="head-container" style="background-color: #6c8be9">-->
            <!--              <el-input v-model="className" placeholder="请输入班级名称" clearable size="small"-->
            <!--                        prefix-icon="el-icon-search" style="margin-bottom: 20px"-->
            <!--              />-->
            <!--            </div>-->
            <div class="head-container">
              <el-tree :data="classOptions" :props="defaultProps" :expand-on-click-node="false"
                       :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
                       @node-click="handleNodeClick"
                       style="background-color: #f3faff;border-radius:  0 0 10px 10px;"
              />
            </div>
          </el-col>
        </pane>
        <!--用户数据-->
        <pane size="84">
          <el-col>
            <el-row :gutter="20">
              <el-col :span="20">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                         label-width="68px"
                >
                  <el-form-item prop="searchValue">
                    <el-input
                      v-model="queryParams.searchValue"
                      placeholder="请输入姓名/学号搜索"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item prop="reportStatus">
                    <el-select class="w-full" v-model="queryParams.reportStatus" placeholder="请选择报道状态">
                      <el-option v-for="dict in dict.type.student_report_status" :key="dict.value" :label="dict.label"
                                 :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item prop="dormBuilding">
                    <el-input
                      v-model="queryParams.dormBuilding"
                      placeholder="选择宿舍楼"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item prop="dormNum">
                    <el-input
                      v-model="queryParams.dormNum"
                      placeholder="选择宿舍房间"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item prop="type">
                    <el-select v-model="queryParams.type" placeholder="请选择学生类别">
                      <el-option v-for="dict in dict.type.student_type" :key="dict.value" :label="dict.label"
                                 :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button size="mini" @click="resetQuery">清除</el-button>
                  </el-form-item>
                </el-form>
              </el-col>
              <el-col :span="4">
                <el-row :gutter="10" style="float: right">
                  <el-col :span="1.5">
                    <el-button
                      type="primary"
                      plain
                      icon="el-icon-plus"
                      size="mini"
                      @click="handleAdd"
                      v-hasPermi="['student:student:add']"
                    >新增
                    </el-button>
                  </el-col>
                  <el-col :span="1.5">
                    <el-button
                      type="warning"
                      plain
                      icon="el-icon-download"
                      size="mini"
                      @click="handleExport"
                      v-hasPermi="['student:student:export']"
                    >导出
                    </el-button>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-table v-loading="loading" :data="studentList" border highlight-current-row>
              <el-table-column label="学号" align="center" prop="studentId"/>
              <el-table-column label="姓名" align="center" prop="name"/>
              <el-table-column label="性别" align="center" prop="gender">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
                </template>
              </el-table-column>
              <el-table-column label="学生类别" align="center" prop="type">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.student_type" :value="scope.row.type"/>
                </template>
              </el-table-column>
              <el-table-column label="所在组织" align="center" prop="faculty">
                <template slot-scope="scope">
                  <span>{{ scope.row.faculty }} > {{ scope.row.major }}</span>
                </template>
              </el-table-column>
              <el-table-column label="班级" align="center" prop="classId"/>
              <el-table-column label="宿舍号" align="center" prop="dormNum">
                <template slot-scope="scope">
                  <span>{{ scope.row.dormBuilding }} - {{ scope.row.dormNum }}</span>
                </template>
              </el-table-column>
              <el-table-column label="报到状态" align="center" prop="reportStatus">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.student_report_status" :value="scope.row.reportStatus"/>
                </template>
              </el-table-column>
              <el-table-column label="报到时间" align="center" prop="reportTime" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.reportTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="学费缴纳状态" align="center" prop="tuitionStatus">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.student_tuition_status" :value="scope.row.tuitionStatus"/>
                </template>
              </el-table-column>
              <el-table-column label="贷款申请状态" align="center" prop="loanStatus">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.student_loan_status" :value="scope.row.loanStatus"/>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="380" class-name="small-padding fixed-width"
                               fixed="right"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    plain
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['student:student:edit']"
                  >编辑
                  </el-button>
                  <el-button
                    size="mini"
                    type="primary"
                    plain
                    icon="el-icon-view"
                    @click="handlePreview(scope.row)"
                    v-hasPermi="['student:student:query']"
                  >查看
                  </el-button>
                  <el-button
                    size="mini"
                    type="info"
                    plain
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['student:student:remove']"
                  >删除
                  </el-button>
                  <el-button
                    size="mini"
                    type="info"
                    plain
                    @click="handleCancelReport(scope.row)"
                    v-hasPermi="['student:student:edit']"
                  >取消报道
                  </el-button>
                  <el-button
                    size="mini"
                    type="info"
                    plain
                    @click="handleCancelDorm(scope.row)"
                    v-hasPermi="['student:student:edit']"
                  >取消宿舍
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>
    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="preview">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学号" prop="studentId">
              <el-input v-model="form.studentId" placeholder="请输入学号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select class="w-full" v-model="form.gender" placeholder="请选择性别">
                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                           :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别" prop="type">
              <el-select class="w-full" v-model="form.type" placeholder="请选择类别">
                <el-option v-for="dict in dict.type.student_type" :key="dict.value" :label="dict.label"
                           :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属院系" prop="faculty">
              <el-input v-model="form.faculty" placeholder="请输入所属院系"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属专业" prop="major">
              <el-input v-model="form.major" placeholder="请输入所属专业"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">

          <el-col :span="12">
            <el-form-item label="所属班级" prop="classId">
              <el-input v-model="form.classId" placeholder="请输入所属班级"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分配的宿舍楼" prop="dormBuilding">
              <el-input v-model="form.dormBuilding" placeholder="请输入分配的宿舍楼"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">

          <el-col :span="12">
            <el-form-item label="分配的宿舍号" prop="dormNum">
              <el-input v-model="form.dormNum" placeholder="请输入分配的宿舍号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报道状态" prop="reportStatus">
              <el-select class="w-full" v-model="form.reportStatus" placeholder="请选择报道状态">
                <el-option v-for="dict in dict.type.student_report_status" :key="dict.value" :label="dict.label"
                           :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="报到时间" prop="reportTime">
              <el-date-picker clearable
                              class="w-full"
                              v-model="form.reportTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择报到时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学费缴纳状态" prop="tuitionStatus">
              <el-select class="w-full" v-model="form.tuitionStatus" placeholder="请选择学费缴纳状态">
                <el-option v-for="dict in dict.type.student_tuition_status" :key="dict.value" :label="dict.label"
                           :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="贷款申请状态" prop="loanStatus">
              <el-select class="w-full" v-model="form.loanStatus" placeholder="请选择贷款申请状态态">
                <el-option v-for="dict in dict.type.student_loan_status" :key="dict.value" :label="dict.label"
                           :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" show-word-limit maxlength="200"/>
        </el-form-item>
      </el-form>
      <div v-if="!preview" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent } from '@/api/student/student'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { Splitpanes, Pane } from 'splitpanes'
import 'splitpanes/dist/splitpanes.css'
import { classTree } from '@/api/student/class'

export default {
  name: 'Student',
  dicts: ['sys_user_sex', 'student_report_status', 'student_tuition_status', 'student_loan_status', 'student_type'],
  components: { Treeselect, Splitpanes, Pane },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      studentList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        name: null,
        gender: null,
        type: null,
        faculty: null,
        major: null,
        classId: null,
        dormBuilding: null,
        dormNum: null,
        reportStatus: null,
        reportTime: null,
        tuitionStatus: null,
        loanStatus: null,
        searchValue: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentId: [
          { required: true, message: '学号不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '性别不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '类别(专科, 本科, 研究生)不能为空', trigger: 'change' }
        ],
        faculty: [
          { required: true, message: '所属院系不能为空', trigger: 'blur' }
        ],
        major: [
          { required: true, message: '所属专业不能为空', trigger: 'blur' }
        ],
        classId: [
          { required: true, message: '所属班级不能为空', trigger: 'blur' }
        ],
        reportTime: [
          { required: true, message: '报到时间不能为空', trigger: 'blur' }
        ]
      },

      classOptions: [],
      className: null,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      preview: false
    }
  },
  created() {
    this.getList()
    this.getClassTree()
  },
  methods: {
    /** 查询学生列表 */
    getList() {
      this.loading = true
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询部门下拉树结构 */
    getClassTree() {
      classTree().then(response => {
        this.classOptions = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentId: null,
        name: null,
        gender: null,
        type: null,
        faculty: null,
        major: null,
        classId: null,
        dormBuilding: null,
        dormNum: null,
        reportStatus: null,
        reportTime: null,
        tuitionStatus: null,
        loanStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.classId = null
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.preview = false
      this.open = true
      this.title = '添加学生'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.preview = false
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改学生'
      })
    },
    handlePreview(row) {
      this.reset()
      const id = row.id || this.ids
      this.preview = true
      getStudent(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '查看学生'
        this.preview = true
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除学生编号为"' + ids + '"的数据项？').then(function() {
        return delStudent(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('student/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.classId = data.id
      this.handleQuery()
    },
    handleCancelReport(row) {
      this.$modal.confirm('确认取消学号为"' + row.studentId + '"的报到？').then(function() {
        row.reportStatus = '0'
        return updateStudent(row)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('取消报到成功')
      }).catch(() => {
      })
    },
    handleCancelDorm(row) {
      this.$modal.confirm('确认取消学号为"' + row.studentId + '"的宿舍？').then(function() {
        row.dormBuilding = ''
        row.dormNum = ''
        return updateStudent(row)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('取消宿舍成功')
      }).catch(() => {
      })
    }
  }
}
</script>
<style scoped lang="scss">
.pane-left {
  border-radius: 10px;
  overflow: hidden;
}
</style>
